package com.angel.web.sys;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.angel.core.util.JsonUtil;
import com.angel.core.util.Pager;
import com.angel.core.util.TreeNodes;
import com.angel.health.SysArea;
import com.angel.health.SysAreaExt;
import com.angel.provider.sys.SysManagerAreaProvider;

/**
 * 高亚林
 * 2016年9月20日下午1:07:56
 */

@RequestMapping("/manag/area")
@Controller
public class SysManagerAreaController {

	@Autowired
	private SysManagerAreaProvider provider;
	
	//查询所有区域
	@RequestMapping(value="findAllArea",produces = "application/json; charset=utf-8")
	@ResponseBody
	public String findAllArea(SysArea area){
		List<SysArea> list = provider.findAllArea(area);
		List<TreeNodes> treeList = new ArrayList<TreeNodes>();
		for (SysArea sysArea : list) {
			TreeNodes node = new TreeNodes();
			node.setId(sysArea.getId());
			node.setpId(sysArea.getParentId());
			node.setpIds(sysArea.getParentIds());
			node.setName(sysArea.getName());
			treeList.add(node);
		}
		return JsonUtil.toJson(treeList);
	}
	
	//分页查询
	@RequestMapping("/index")
	public String index(SysArea area,Pager pagers,HttpServletRequest request){
		Pager pager = provider.findAll(area, pagers);
		request.setAttribute("pager", pager);
		return "manag/area";
	}
	
	//批量删除
	@RequestMapping("delete")
	@ResponseBody
	public String delete(String ids){
		int i = provider.delete(ids);
		return JsonUtil.toJson(i);
	}
	
	//删除单个用户
	@RequestMapping("deleteById")
	@ResponseBody
	public String deleteById(SysArea area){
		int i = provider.deleteById(area);
		return JsonUtil.toJson(i);
	}
	
	//保存
	@RequestMapping("/save")
	@ResponseBody
	public String save(SysArea area,HttpServletRequest request){
		area.setCreateBy(request.getSession().getAttribute("user").toString());
		Map<String, String> map = provider.save(area);
		return JsonUtil.toJson(map);
	}
	
	//根据id获取数据
	@RequestMapping("/edit")
	@ResponseBody
	public String edit(SysArea area){
		SysAreaExt ext = provider.findUserById(area);
		return JsonUtil.toJson(ext);
	}
	
	//修改
	@RequestMapping("/update")
	@ResponseBody
	public String update(SysArea area,HttpServletRequest request){
		area.setUpdateBy(request.getSession().getAttribute("user").toString());
		Map<String, String> map = provider.update(area);
		return JsonUtil.toJson(map);
	}
	
	//按级别查询区域 2:省份  3:市  4:区县
	@RequestMapping(value="findArea")
	@ResponseBody
	public Map<String, Object> findArea(SysArea area){
		
		Map<String, Object> map = new HashMap<>();
		
		List<Map<String,Object>> list = provider.findAll(area);
		
		map.put("data", list);
		
		map.put("status", "success");
		map.put("msg", "获取成功");
		
		return map;
	}
	
}
